import QtQuick 2.15
import QtQuick.Layouts 1.15
import QtQuick.Controls 2.15
import QtQuick.Shapes 1.15

MainView {
    id: root
    width: 800
    height: 391
    property color btnColor: "#7080C0"
    property int btnHeight: 50
    property int btnWidth: 110
    Component.onDestruction: {
       funcTimer.destroy()
    }
    function getAddr(){
        var lst=[];
        for(let i=1;i<256;i++){
            lst.push(i)
        }
        return lst
    }
    function savePara()
    {
        var parityValue = 0;
        if(cmbParity.displayText===qsTr("奇校验")){
            parityValue=2
        }
        else if(cmbParity.displayText===qsTr("偶校验")){
            parityValue=1
        }
        var result = mymodbus.slotSaveComParas_new(cmbComName.displayText,Number(cmbBaudrate.displayText),Number(cmbDataBit.displayText),cmbStopBit.currentIndex,parityValue,Number(cmbSlaveId.currentText), 0)
        if(result)
        {
            //comSettingSaveOK=true;
            commParaSaveOKDialog.title=qsTr("保存成功")
            commParaSaveOKDialog.open();
            commParaSaveOKDialog.accepted.connect(function() {
                commParaSaveOKDialog.close()

            })
        }
        else
        {
            commParaSaveOKDialog.title=qsTr("保存失败")
            commParaSaveOKDialog.accepted.connect(function() {
                commParaSaveOKDialog.close()
            })
        }
        loaderText.visible =false
        commParaSaveOKDialog.open()
    }
//    property bool comSettingSaveOK:true
//    property bool startSave:false
    function getParityIndex(parityIndex){
        var index = -1;
        if(parityIndex === 1)
        {
            //return qsTr("偶校验")
            index = 1
        }
        else if(parityIndex === 2)
        {
            index = 2
        }
        else
        {
            index = 0
        }
        return index
    }

    MyBuzyIndicator{
        anchors.centerIn: parent
        id: commParaLoader
        visible:false
        z:101
    }
//    MyComboBox
//    {
//        id:cmbType
//        x: 334
//        y: 32
//        width: btnWidth
//        height: btnHeight
//        borderColor:"white"
//        fillColor:"#0A3653"
//        // background: {
//        //     Rectangle: {
//        //         color:"transparent"
//        //     }
//        // }

//        model:[qsTr("仪表"),qsTr("编码器")]
//        currentIndex: 0
//        onActivated: {
//            if(cmbType.currentIndex === 0){
//                cmbComName.currentIndex = cmbComName.find(mymodbus.comName);
//                cmbBaudrate.currentIndex = cmbBaudrate.find(mymodbus.baudrate);
//                cmbDataBit.currentIndex = cmbDataBit.find(mymodbus.dataBit);
//                cmbStopBit.currentIndex = mymodbus.stopBit;
//                cmbParity.currentIndex =getParityIndex(mymodbus.parity)
//                cmbSlaveId.currentIndex = cmbSlaveId.find(mymodbus.sensorSlaveID)
//            }
//            else if(cmbType.currentIndex === 1){
//                cmbComName.currentIndex = cmbComName.find(mymodbus.comNameEncoder);
//                cmbBaudrate.currentIndex = cmbBaudrate.find(mymodbus.baudrateEncoder);
//                cmbDataBit.currentIndex = cmbDataBit.find(mymodbus.dataBitEncoder);
//                cmbStopBit.currentIndex = mymodbus.stopBitEncoder;
//                cmbParity.currentIndex =getParityIndex(mymodbus.parityEncoder)
//                cmbSlaveId.currentIndex = cmbSlaveId.find(mymodbus.encoderSlaveID)
//            }
//        }
//    }
    Component.onCompleted: {
        cmbComName.currentIndex = cmbComName.find(mymodbus.comName);
        cmbBaudrate.currentIndex = cmbBaudrate.find(mymodbus.baudrate);
        cmbDataBit.currentIndex = cmbDataBit.find(mymodbus.dataBit);
        cmbStopBit.currentIndex = mymodbus.stopBit;
        cmbParity.currentIndex =getParityIndex(mymodbus.parity)
        cmbSlaveId.currentIndex = cmbSlaveId.find(mymodbus.sensorSlaveID)
    }
    GridLayout{
        id: generalSettingsLayout
        rows: 2
        columns: 6
        anchors.centerIn: parent
        rowSpacing: 40
        Button
        {
            Layout.row:0
            Layout.column:0
            Layout.preferredWidth: btnWidth
            Layout.preferredHeight: btnHeight
            // x: 152
            // y: 13
            text: qsTr("串口号")
            font.pixelSize: 18
            background: Rectangle{
                color: btnColor
                    }
            onClicked: {
                cmbComName.popup.open()
                //miniKeyBoard.lineValue = "";
                //miniKeyBoard.textInput=oldPwd;
            }
        }

       MyComboBox
       {
           id:cmbComName
           Layout.row:0
           Layout.column:1
           Layout.preferredWidth: btnWidth
           Layout.preferredHeight: btnHeight
           borderColor:"white"
           fillColor:"#0A3653"

           model:mymodbus.comData
           currentIndex: cmbComName.find(mymodbus.comName)
           //displayText: mymodbus.comName
           onActivated: {
               //cmbComName.displayText=cmbComName.currentText;
           }

       }
            Button
            {
                Layout.row:0
                Layout.column:2
                Layout.preferredWidth: btnWidth
                Layout.preferredHeight: btnHeight
                // x: 152
                // y: 13
                text: qsTr("波特率")
                font.pixelSize: 18
                background: Rectangle{
                    color: btnColor
                        }
                onClicked: {
                    cmbBaudrate.popup.open()
                    //miniKeyBoard.lineValue = "";
                    //miniKeyBoard.textInput=oldPwd;
                }
            }
            MyComboBox
            {
                id:cmbBaudrate
                Layout.row:0
                Layout.column:3
                Layout.preferredWidth: btnWidth
                Layout.preferredHeight: btnHeight
                borderColor:"white"
                fillColor:"#0A3653"
                // background: {
                //     Rectangle: {
                //         color:"transparent"
                //     }
                // }

                model:[9600,19200,38400,57600,115200]
                //displayText: mymodbus.baudrate
                currentIndex:cmbBaudrate.find(mymodbus.baudrate)
                onActivated: {
                    //cmbBaudrate.displayText=cmbBaudrate.currentText;
                }
            }
            Button
            {
                Layout.row:0
                Layout.column:4
                Layout.preferredWidth: btnWidth
                Layout.preferredHeight: btnHeight
                // x: 152
                // y: 13
                text: qsTr("数据位")
                font.pixelSize: 18
                background: Rectangle{
                    color: btnColor
                        }
                onClicked: {
                    cmbDataBit.popup.open()
                    //miniKeyBoard.lineValue = "";
                    //miniKeyBoard.textInput=oldPwd;
                }
            }

            MyComboBox
            {
                id:cmbDataBit
                Layout.row:0
                Layout.column:5
                Layout.preferredWidth: btnWidth
                Layout.preferredHeight: btnHeight
                borderColor:"white"
                fillColor:"#0A3653"
                // background: {
                //     Rectangle: {
                //         color:"transparent"
                //     }
                // }

                model:[5,6,7,8]
                //displayText: mymodbus.dataBit.toString()
                currentIndex:cmbDataBit.find(mymodbus.dataBit)
                onActivated: {
                    //cmbDataBit.displayText=cmbDataBit.currentText;
                }
            }

            Button
            {
                Layout.row:1
                Layout.column:0
                Layout.preferredWidth: btnWidth
                Layout.preferredHeight: btnHeight
                // x: 152
                // y: 13
                text: qsTr("停止位")
                font.pixelSize: 18
                background: Rectangle{
                    color: btnColor
                        }
                onClicked: {
                    cmbStopBit.popup.open()
                    //miniKeyBoard.lineValue = "";
                    //miniKeyBoard.textInput=oldPwd;
                }
            }

            MyComboBox
            {
                id:cmbStopBit
                Layout.row:1
                Layout.column:1
                Layout.preferredWidth: btnWidth
                Layout.preferredHeight: btnHeight
                borderColor:"white"
                fillColor:"#0A3653"
                // background: {
                //     Rectangle: {
                //         color:"transparent"
                //     }
                // }

                model:[1,1.5,2]
                currentIndex: mymodbus.stopBit
                //displayText: mymodbus.stopBit.toString()
                onActivated: {
                    cmbStopBit.displayText=cmbStopBit.currentText;
                }
            }

            Button
            {
                Layout.row:1
                Layout.column:2
                Layout.preferredWidth: btnWidth
                Layout.preferredHeight: btnHeight
                // x: 152
                // y: 13
                text: qsTr("校验位")
                font.pixelSize: 18
                background: Rectangle{
                    color: btnColor
                        }
                onClicked: {
                    cmbParity.popup.open()
                    //miniKeyBoard.lineValue = "";
                    //miniKeyBoard.textInput=oldPwd;
                }
            }
            MyComboBox
            {
                id:cmbParity
                Layout.row:1
                Layout.column:3
                Layout.preferredWidth: btnWidth
                Layout.preferredHeight: btnHeight
                borderColor:"white"
                fillColor:"#0A3653"
                // background: {
                //     Rectangle: {
                //         color:"transparent"
                //     }
                // }

                model:[qsTr("无校验"),qsTr("偶校验"),qsTr("奇校验")]
//                displayText: {
//                    if(mymodbus.parity === 1)
//                    {
//                        return qsTr("偶校验")
//                    }
//                    else if(mymodbus.parity === 2)
//                    {
//                        return qsTr("奇校验")
//                    }
//                    else
//                    {
//                        return qsTr("无校验")
//                    }
//                }
                currentIndex: getParityIndex(mymodbus.parity)
                onActivated: {
                    //cmbParity.displayText=cmbParity.currentText;
                }
            }
            Button
            {
                Layout.row:1
                Layout.column:4
                Layout.preferredWidth: btnWidth
                Layout.preferredHeight: btnHeight
                // x: 152
                // y: 13
                text: qsTr("通道号")
                font.pixelSize: 18
                background: Rectangle{
                    color: btnColor
                        }
                onClicked: {
                    cmbSlaveId.popup.open()
                    //miniKeyBoard.lineValue = "";
                    //miniKeyBoard.textInput=oldPwd;
                }
            }
            MyComboBox
            {
                id:cmbSlaveId
                Layout.row:1
                Layout.column:5
                Layout.preferredWidth: btnWidth
                Layout.preferredHeight: btnHeight
                borderColor:"white"
                fillColor:"#0A3653"
                // background: {
                //     Rectangle: {
                //         color:"transparent"
                //     }
                // }

                model:getAddr()
                //currentIndex: mymodbus.slaveID
                    //cmbSlaveId.find(mymodbus.slaveID.toString())
            }
    }
    Button
    {
        id:btnCommlOK
        x: 572
        y: 281
        //x: 675
        //y: 20
        width: 100
        height: 100
        background: Rectangle{
            border.width: 0
            color: "#C070B0"
                }

        text: qsTr("确定")
        font.pixelSize: 25
        palette.buttonText: "white"
        onClicked: {
            if(loaderText.visible)
            {
                return;
            }
            loaderText.visible=true;
            funcTimer.start();
        }
    }
    Dialog {
        anchors.centerIn: parent
        id: commParaSaveOKDialog
        //title: "提示"
        standardButtons: Dialog.Ok
        title:qsTr("保存成功！")
        modal: true
        closePolicy: "CloseOnEscape"
//        onAccepted: {

//        }
        z:100
        //onRejected: console.log("Cancel clicked")
    }
    Button
    {
        id:btnReturn
        x: 688
        y: 281
        //x: 675
        //y: 20
        width: 100
        height: 100
        background: Rectangle{
            border.width: 0
            color: "#B0B0D0"
                }

        text: qsTr("返回")
        font.pixelSize: 25
        palette.buttonText: "white"
        onClicked: {
            bottomBar.selected=4
//            myLoader.visible=false;
//            myLoader.visible=true
            myLoader.source="SettingsPanel.qml"
            //funcTimer.destroy()
        }
    }
    Timer {
        id:funcTimer
                running: false
                repeat: false
                interval:100
                onTriggered: {
                    savePara()
                }
            }
}

